package cn.neusoft.controller;

import cn.neusoft.domain.Allocate;
import cn.neusoft.domain.Warehouse;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.feignClients.DeliveryFeignClient;
import cn.neusoft.feignClients.WarehouseFeignClient;
import cn.neusoft.service.WarehouseService;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/warehouses")
@Api(value = "库房模块的API",description="库房相关的CRUD功能")
public class WarehouseController {

    @Autowired
    private final WarehouseService warehouseService;

    @Autowired
    DeliveryFeignClient deliveryFeignClient;
    @Autowired
    WarehouseFeignClient warehouseFeignClient;

    @Autowired
    public WarehouseController(WarehouseService warehouseService) {
        this.warehouseService = warehouseService;
    }

//    @PostMapping
//    @ApiOperation(value = "添加库房" )
//    public void addWarehouse(@RequestBody Warehouse warehouse) {
//        warehouseService.add(warehouse);
//    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除库房" )
    public void deleteWarehouse(@PathVariable("id") Long warehouseId) {
        warehouseService.delete(warehouseId);
    }

    @PutMapping("mod/{id}")
    @ApiOperation(value = "修改库房" )
    public void updateWarehouse(@PathVariable("id") Long warehouseId, @RequestBody Warehouse warehouse) {
        warehouse.setWarehouse_id(warehouseId);
        warehouseService.update(warehouse);
    }

    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有库房" )
    public List<Warehouse> getAllWarehouses() {
        return warehouseService.getAll();
    }

    @GetMapping("/search/{id}")
    @ApiOperation(value = "根据id查询库房" )
    public Warehouse getWarehouseById(@PathVariable("id") Long warehouseId) {
        return warehouseService.getById(warehouseId);
    }

    @GetMapping("/search")
    public List<Warehouse> searchWarehousesByName(@RequestParam("name") String warehouseName) {
        return warehouseService.getByWarehouseName(warehouseName);
    }

    @GetMapping("/page")
    public List<Warehouse> getWarehousesByPage(@RequestParam("start") int start, @RequestParam("pageSize") int pageSize) {
        return warehouseService.getWarehousesByPage(start, pageSize);
    }

    @GetMapping("/count")
    public int getTotalCount() {
        return warehouseService.getTotalCount();
    }

    @PostMapping("/addAllocation")
    public ResultBean addAllocation(@RequestBody Allocate allocate){
        Long inId = allocate.getIn_id();
        Long amount = allocate.getAmount();

        Long stock =  warehouseFeignClient.GetWarehouseStock(inId);
        Warehouse byId = warehouseService.getById(inId);

        if(amount+stock <= byId.getMax_value()){
            deliveryFeignClient.AddAllocate(allocate);
            return ResultBean.success();
        }else {
            return ResultBean.error(AppExceptionCodeMsg.WAREHOUSE_EXCEED_MaxVALUE);
        }
    }

    @ApiOperation(value = "获取中央库房的ID")
    @GetMapping("/warehouse/getCenterWarehouseID")
    List<Warehouse> getCenterWarehouseID(){
        int level = 1;
        return warehouseService.getWarehouseByLevel(level);
    }
}